package com.example.controller;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.*;

public class TestThread {
    //ExecutorService executorService = Executors.newFixedThreadPool(3);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(3));

    public void test() {
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 0; i < 50; i++) {
            queue.add(i);
        }

        while (!queue.isEmpty()) {
            Integer num = queue.poll();
            System.out.printf("%s pull %d", Thread.currentThread().getName(), num);
            System.out.println();
            threadPoolExecutor.submit(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.printf("%s finish %d", Thread.currentThread().getName(), num);
                System.out.println();
            });
        }
    }

    public static void main(String[] args) {
        new TestThread().test();
    }
}
